# !/usr/bin/env python
# !-*-coding:utf-8 -*-
# !@TIME: 
# !@Author : Destroy丶寒露
# !@File :.py

from selenium import webdriver
import time
from spider import FilmTop, Lagou, JD, Biquge


# 创建模拟浏览器对象
class VisitWeb:

    def __init__(self, url, *key):  # self,爬虫网址,driver路径
        self.url = url
        self.data_num = 1
        self.driver = webdriver.Chrome()  # self.set_option(loc)
        self.web = self.choose_web()
        self.info_list = []
        self.key = key

    def set_option(self, application_loc):  # 语法：driver = setOption(application_loc)
        self.driver.quit()
        option = webdriver.ChromeOptions()
        option.binary_location = r'%s\chrome.exe' % application_loc
        chrome_driver_location = r'%s\chromedriver' % application_loc
        self.driver = webdriver.Chrome(chrome_driver_location, options=option)
        self.web = self.choose_web()

    def choose_web(self):
        if self.url == 'https://www.maoyan.com/board/4':
            return FilmTop.FilmTop(self.driver)
        elif self.url == 'https://www.lagou.com/':
            return Lagou.Lagou(self.driver)
        elif self.url == 'https://www.jd.com/':
            return JD.JD(self.driver)
        elif self.url == 'http://www.xbiquge.la/paihangbang/':
            return Biquge.Biquge(self.url)

    def visit_web(self):  # 'https://maoyan.com/board/4'
        self.driver.get(self.url)
        time.sleep(5)
        self.web.start(self)
        #get_movie(driver, i, info_list)
        return self.info_list

    # 写txt文件
    def write_in_txt(self, txt_name, data):
        with open(txt_name + '.txt', mode='a', encoding='utf-8') as f:
            f.write(data)

# main
# webdrv = VisitWeb('https://maoyan.com/board/4') # https://www.lagou.com/  # https://maoyan.com/board/4
# webdrv.set_option('C:\Program Files (x86)\Google\Chrome\Application')
# webdrv.visit_web()
# print(webdrv.info_list)


